<?php
use Pinenut\Form;

class IndexAction extends \Action
{
	protected static $layout = "#layout";

	/**
	 * Указываем необходимые неявные параметры
	 * @return multitype:string
	 */
	static function pullParams() {
		return ['sessionId', 'isPost', 'isAjax'];
	}
	
	protected function run()
	{
		
		// отрубаем кеширование если пост-запрос
		if ($this->request->_isPost) {
			$this->response->noCache(true);
		}
		
		$formId = 'formId';
		$formConfig = \FormConfig::get();

		$form = new Form($this->request->_sessionId, $formId, $formConfig);

		if ($this->request->_isPost && $this->request->_isAjax) {
			$form->process($this->request->params(), true);
			// вынести в хелпер
			$this->response->content =  $form->render();
			$this->response->noRender(true);
			return;


		} else if ($this->request->_isPost && !$this->request->_isAjax) {
			if ($form->process($this->request->params())) {
				//save
				$form->flush();
				$this->redirect('/');
			}
		}

		$this->response->form = $form;
	}
}